import java.util.List;

/**
 * Class for an AI playing 5 card draw without betting
 * @author Kat Winter
 */
public class PokerAI {

	private PokerGame game = new PokerGame();

	/**
	 * Logic for the AI evaluating its hand, exchanging cards based
	 * on the value of its hand, and reporting back how many cards it
	 * exchanged
	 * @param dealer Player representing the AI
	 * @return Int for the number of cards which the AI swapped out
	 */
	public int play(Player dealer) {

		List<Card> dealerHand = dealer.getHand();

		// Higher values represent a better hand
		TypeOfHand dealerValue = game.evaluateHand(dealer);


		switch (dealerValue) {
		
		// If its a Four of a Kind, swap out the 5th card if its value is less than 10
		case FOUR_OF_A_KIND: {

			dealer.removeCard(dealerHand.get(0));
			return 1;
		}
		
		// If it's Three of a Kind, swap out the lowest of the remaining two cards
		case THREE_OF_A_KIND: {

			if (dealerHand.get(0).compareTo(dealerHand.get(1)) > 0) {
				dealer.removeCard(dealerHand.get(1));
			}

			else {

				dealer.removeCard(dealerHand.get(0));
			}

			return 1;
		}
		
		// If it's Two Pair, swap out the fifth card if its face value is less than 10
		case TWO_PAIR: {
			
			dealer.removeCard(dealerHand.get(0));
			return 1;
		}
		
		// If it's only a pair, then swap the lowest remaining card
		// Then from the other two unswapped and unpaired cards, swap
		// out the lowest of them
		case PAIR: {

			dealer.removeCard(game.getLowest(dealerHand.subList(0, 3)));
			dealerHand.remove(game.getLowest(dealerHand.subList(0, 3)));
			dealer.removeCard(game.getLowest(dealerHand.subList(0, 2)));
				
			return 2;
		}
		
		// If the hand has no value at all, swap cards based on trying to
		// get straight or flush - and if that doesn't seem likely, then
		// swap out the four lowest cards (just keep the highest valued card)
		case NOTHING: {

			// check for four of the same suit
			if (game.getCountOfSameSuit(dealerHand) == 4) {
				
				dealer.removeCard(dealerHand.get(0));
				return 1;
			}
			
			// check for four in a row
			if (game.hasStraightOfFour(dealerHand)) {

				dealer.removeCard(dealerHand.get(0));
				return 1;
			}
			
			// check for three of the same suit
			if (game.getCountOfSameSuit(dealerHand) == 3) {

				dealer.removeCard(dealerHand.get(1));
				dealer.removeCard(dealerHand.get(0));
				return 2;
			}
			
			// Keep only the highest card
			else {
				
				Card highest = game.getHighCard(dealerHand);
				dealerHand.remove(highest);
				
				for (Card card : dealerHand) {
					dealer.removeCard(card);
				}
				
				return 4;
			}
		}
		}

		return 0;
	}

}
